package com.core.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import com.core.dao.impl.UsernameDAOImpl;
import com.core.entities.Username;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final JLabel lblUsername = new JLabel("Username: ");
	public final JTextField usernameTextField = new JTextField();
	private final JLabel lblPassword = new JLabel("Password :");
	private final JPasswordField passwordField = new JPasswordField();
//	String usernameEntry = usernameTextField.getText();
//	String passwordEntry = passwordField.getText();
	
	ChangePasswordDialog changePasswordDialog;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//
//			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//		} catch (UnsupportedLookAndFeelException e) {
//			// handle exception
//		} catch (ClassNotFoundException e) {
//			// handle exception
//		} catch (InstantiationException e) {
//			// handle exception
//		} catch (IllegalAccessException e) {
//			// handle exception
//		}
//		try {
//			LoginDialog dialog = new LoginDialog();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		usernameTextField.setBounds(69, 8, 145, 20);
		usernameTextField.setColumns(10);
		setBounds(100, 100, 325, 145);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lblUsername.setBounds(10, 11, 55, 14);
		
		contentPanel.add(lblUsername);
		
		contentPanel.add(usernameTextField);
		lblPassword.setBounds(10, 36, 53, 14);
		
		contentPanel.add(lblPassword);
		passwordField.setBounds(69, 33, 145, 20);
		
		contentPanel.add(passwordField);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						validatFields();
						ChangePasswordDialog.usernameLabel.setText(usernameTextField.getText());
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	private void logIn(){
		
		UsernameDAOImpl usernameDAOImpl = new UsernameDAOImpl();
		Username getusername;
		try{
			
			getusername = usernameDAOImpl.findByUsername(usernameTextField.getText());
			getusername = usernameDAOImpl.findByPassword(passwordField.getText());
			if(getusername.getUsername().equals(usernameTextField.getText()) && getusername.getPassword().equals(passwordField.getText())){
				new MainFrame().setVisible(true);
				this.dispose();
			}
			else{
				JOptionPane.showMessageDialog(null, "Username or Password is invalid, Please check it and try again.",null,JOptionPane.ERROR_MESSAGE);
			}
			
		}catch(Exception e){
			
		}
		
		
	}
	private void validatFields(){
String miss = "Missing data of :" + "\n";
if (usernameTextField.getText().isEmpty()|| passwordField.getText().isEmpty()) {

	if (usernameTextField.getText().isEmpty()) {
		miss += "Username.\n";
	}
	if (passwordField.getText().isEmpty()) {
		miss += "Password.\n";
	}

	JOptionPane.showMessageDialog(null, miss, "",
			JOptionPane.ERROR_MESSAGE);

} else {

	logIn();
}
}
}
